<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>课程管理系统</title>
    <style>
        :root {
            --primary-dark: #1a0a2e;
            --secondary-dark: #2d1b45;
            --accent-purple: #bd88ff;
            --accent-violet: #9d4edd;
            --accent-indigo: #7b2cbf;
            --text-light: #f0e6ff;
            --text-muted: #c9a8ff;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, var(--primary-dark) 0%, var(--secondary-dark) 100%);
            color: var(--text-light);
            min-height: 100vh;
            padding: 20px;
            position: relative;
            overflow-x: hidden;
        }

        /* 魔法粒子背景 */
        body::before {
            content: "";
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-image:
                    radial-gradient(2px 2px at 20px 30px, #bd88ff, transparent),
                    radial-gradient(1px 1px at 40px 70px, rgba(157, 78, 221, 0.8), transparent),
                    radial-gradient(1px 1px at 90px 40px, #c9a8ff, transparent),
                    radial-gradient(2px 2px at 160px 30px, rgba(123, 44, 191, 0.6), transparent);
            background-repeat: repeat;
            background-size: 200px 100px;
            animation: sparkle 4s linear infinite;
            opacity: 0.4;
            z-index: -1;
        }

        @keyframes sparkle {
            from { transform: translateY(0); }
            to { transform: translateY(-100px); }
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            position: relative;
            z-index: 1;
        }

        header {
            text-align: center;
            padding: 30px 0;
            margin-bottom: 30px;
            position: relative;
        }

        header::after {
            content: "";
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 200px;
            height: 3px;
            background: linear-gradient(90deg, transparent, var(--accent-violet), transparent);
            border-radius: 50%;
        }

        header h1 {
            font-weight: 400;
            font-size: 2.8rem;
            background: linear-gradient(90deg, var(--accent-purple), var(--accent-indigo));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            letter-spacing: 3px;
            text-shadow: 0 0 15px rgba(189, 136, 255, 0.5);
            margin-bottom: 10px;
        }

        header p {
            color: var(--text-muted);
            font-size: 1.1rem;
            letter-spacing: 1px;
        }

        .card {
            background: rgba(43, 15, 65, 0.7);
            border-radius: 15px;
            padding: 25px;
            margin-bottom: 30px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
            border: 1px solid rgba(189, 136, 255, 0.2);
            backdrop-filter: blur(10px);
            position: relative;
            overflow: hidden;
        }

        .card::before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 3px;
            background: linear-gradient(90deg, transparent, var(--accent-violet), transparent);
        }

        .card-title {
            font-size: 1.6rem;
            margin-bottom: 20px;
            color: var(--accent-purple);
            font-weight: 400;
            display: flex;
            align-items: center;
        }

        .card-title::before {
            content: "✦";
            margin-right: 10px;
            color: var(--accent-violet);
        }

        .form-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: var(--text-muted);
            font-size: 0.9rem;
            letter-spacing: 0.5px;
        }

        .form-group input {
            width: 100%;
            padding: 12px 15px;
            border: none;
            border-radius: 8px;
            background: rgba(30, 10, 45, 0.7);
            color: var(--text-light);
            box-sizing: border-box;
            border: 1px solid rgba(189, 136, 255, 0.2);
            transition: all 0.3s ease;
        }

        .form-group input:focus {
            outline: none;
            border-color: var(--accent-purple);
            box-shadow: 0 0 15px rgba(189, 136, 255, 0.4);
        }

        .button-group {
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
        }

        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-weight: 500;
            transition: all 0.3s ease;
            background: linear-gradient(135deg, var(--accent-violet) 0%, var(--accent-indigo) 100%);
            color: white;
            border: none;
            position: relative;
            overflow: hidden;
            letter-spacing: 0.5px;
        }

        .btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(123, 44, 191, 0.6);
        }

        .btn::after {
            content: "";
            position: absolute;
            top: -50%;
            left: -60%;
            width: 20px;
            height: 200%;
            background: rgba(255, 255, 255, 0.3);
            transform: rotate(25deg);
            transition: all 0.6s;
        }

        .btn:hover::after {
            left: 120%;
        }

        .btn-secondary {
            background: rgba(30, 10, 45, 0.7);
            border: 1px solid rgba(189, 136, 255, 0.2);
            color: var(--text-light);
        }

        .btn-secondary:hover {
            background: rgba(43, 15, 65, 0.7);
            box-shadow: 0 5px 15px rgba(189, 136, 255, 0.3);
        }

        .search-container {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
        }

        .search-container input {
            flex: 1;
            padding: 12px 15px;
            border: none;
            border-radius: 8px;
            background: rgba(30, 10, 45, 0.7);
            color: var(--text-light);
            border: 1px solid rgba(189, 136, 255, 0.2);
        }

        .table-container {
            overflow-x: auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background: rgba(43, 15, 65, 0.5);
            border-radius: 10px;
            overflow: hidden;
        }

        th, td {
            padding: 15px;
            text-align: left;
            border-bottom: 1px solid rgba(189, 136, 255, 0.1);
        }

        th {
            background: rgba(30, 10, 45, 0.7);
            color: var(--accent-purple);
            font-weight: 500;
        }

        tr:hover {
            background: rgba(123, 44, 191, 0.15);
        }

        .action-btn {
            padding: 6px 12px;
            margin: 0 5px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 0.9rem;
            transition: all 0.2s ease;
        }

        .edit-btn {
            background: rgba(123, 44, 191, 0.2);
            color: var(--accent-violet);
        }

        .delete-btn {
            background: rgba(255, 77, 77, 0.2);
            color: #ff4d4d;
        }

        .action-btn:hover {
            transform: translateY(-1px);
            box-shadow: 0 3px 8px rgba(0, 0, 0, 0.2);
        }

        .edit-btn:hover {
            background: rgba(123, 44, 191, 0.3);
        }

        .delete-btn:hover {
            background: rgba(255, 77, 77, 0.3);
        }

        #batchActions {
            margin-top: 10px;
            display: none;
        }

        .course-checkbox {
            display: none;
        }

        #selectAll {
            display: none;
        }

        /* 魔法悬浮动画 */
        .card, .btn, .action-btn {
            transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.5);
        }

        @media (max-width: 768px) {
            .form-grid {
                grid-template-columns: 1fr;
            }

            .search-container {
                flex-direction: column;
            }

            .button-group {
                flex-direction: column;
            }

            .btn {
                width: 100%;
            }

            header h1 {
                font-size: 2rem;
            }
        }

        /* 魔法粒子装饰 */
        .magic-particle {
            position: absolute;
            font-size: 18px;
            opacity: 0.7;
            animation: floatParticle 8s infinite ease-in-out;
            z-index: -1;
        }

        @keyframes floatParticle {
            0% { transform: translate(0, 0) rotate(0deg); opacity: 0.3; }
            50% { transform: translate(10px, -20px) rotate(180deg); opacity: 0.7; }
            100% { transform: translate(0, 0) rotate(360deg); opacity: 0.3; }
        }
    </style>
</head>
<body>
<!-- 装饰性魔法粒子 -->
<div class="magic-particle" style="top: 10%; left: 5%; animation-delay: 0s;">✦</div>
<div class="magic-particle" style="top: 20%; right: 10%; animation-delay: 2s;">✧</div>
<div class="magic-particle" style="bottom: 30%; left: 15%; animation-delay: 4s;">★</div>
<div class="magic-particle" style="bottom: 15%; right: 20%; animation-delay: 6s;">✦</div>

<div class="container">
    <header>
        <h1>课程管理系统</h1>
        <p>探索知识的奥秘，解锁智慧的力量</p>
    </header>

    <!-- 添加/编辑表单 -->
    <div class="card">
        <h2 class="card-title" id="formTitle">添加课程</h2>
        <div class="form-grid">
            <input type="hidden" id="courseId">
            <div class="form-group">
                <label>课程名:</label>
                <input type="text" id="cname" placeholder="课程名称">
            </div>
            <div class="form-group">
                <label>简介:</label>
                <input type="text" id="mark" placeholder="课程简介">
            </div>
            <div class="form-group">
                <label>教师:</label>
                <input type="text" id="tea" placeholder="授课教师">
            </div>
        </div>
        <div class="button-group">
            <button class="btn" onclick="saveCourse()">保存课程</button>
            <button class="btn btn-secondary" onclick="cancelEdit()">取消</button>
        </div>
    </div>

    <!-- 搜索和操作 -->
    <div class="card">
        <h2 class="card-title">课程搜索</h2>
        <div class="search-container">
            <input type="text" id="searchKeyword" placeholder="输入关键词搜索课程...">
            <button class="btn" onclick="searchCourse()">搜索</button>
            <button class="btn btn-secondary" onclick="showAllCourse()">显示全部</button>
        </div>
    </div>

    <!-- 操作面板 -->
    <div class="card">
        <h2 class="card-title">操作面板</h2>
        <div class="button-group">
            <button class="btn" onclick="showStatistics()">查看统计</button>
            <button class="btn" onclick="exportCourses()">导出数据</button>
            <button class="btn btn-secondary" onclick="toggleBatchMode()">批量操作</button>
        </div>
        <div id="batchActions">
            <button class="btn delete-btn" onclick="batchDelete()">批量删除</button>
        </div>
    </div>

    <!-- 课程列表 -->
    <div class="card">
        <h2 class="card-title">课程列表</h2>
        <div class="table-container">
            <table>
                <thead>
                <tr>
                    <th><input type="checkbox" id="selectAll" onclick="toggleSelectAll()"></th>
                    <th>序号</th>
                    <th>课程名称</th>
                    <th>简介</th>
                    <th>教师</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="tb">
                </tbody>
            </table>
        </div>

        <!-- 智能客服小窗 -->
        <div id="chatWidget" class="chat-widget">
            <div class="chat-header" onclick="toggleChatWidget()">
                <span>智能客服助手</span>
                <span id="chatToggleIcon">▲</span>
            </div>
            <div class="chat-content" id="chatContent">
                <div class="chat-messages" id="chatMessages">
                    <div class="message bot-message">
                        您好！我是智能客服助手<br><br>
                        <strong>支持的操作：</strong><br>
                        • 输入"统计" - 查看课程统计<br>
                        • 输入"添加课程" - 添加新课程<br>
                        • 输入"修改课程 ID 字段:新值" - 修改课程<br>
                        • 输入"删除课程 ID" - 删除课程<br><br>

                        <strong>示例：</strong><br>
                        添加课程 名称:数学 教师:张老师 简介:基础数学课程
                    </div>
                </div>
            </div>
                <div class="chat-input">
                    <input type="text" id="widgetMessageInput" placeholder="请输入您的问题..." onkeypress="handleWidgetKeyPress(event)">
                    <button onclick="sendWidgetMessage()">发送</button>
                </div>
            </div>
        </div>
</div>

        <script>
            // 智能客服小窗功能
            function toggleChatWidget() {
                const content = document.getElementById('chatContent');
                const icon = document.getElementById('chatToggleIcon');
                if (content.style.display === 'none') {
                    content.style.display = 'block';
                    icon.textContent = '▲';
                } else {
                    content.style.display = 'none';
                    icon.textContent = '▼';
                }
            }

            function sendWidgetMessage() {
                const messageInput = document.getElementById('widgetMessageInput');
                const message = messageInput.value.trim();

                if (!message) return;

                // 显示用户消息
                appendWidgetMessage(message, 'user');
                messageInput.value = '';

                // 发送请求到客服后端
                $.ajax({
                    url: '/support/message',
                    method: 'GET',
                    data: { message: message },
                    dataType: 'json',
                    success: function(response) {
                        console.log("客服返回:", response);

                        if (response.action && response.action.type === "statistics") {
                            // 处理统计信息
                            const statsData = response.action.data;
                            if (statsData) {
                                const totalCourses = statsData.totalCourses || 0;
                                const coursesByTeacher = statsData.coursesByTeacher || {};

                                let statsText = "课程统计信息如下<br>";
                                statsText += `总课程数：${totalCourses} 门<br>`;
                                statsText += "各教师课程数量：<br>";

                                const teacherEntries = Object.entries(coursesByTeacher);
                                if (teacherEntries.length === 0) {
                                    statsText += "• 暂无教师数据";
                                } else {
                                    teacherEntries.forEach(([name, count], index) => {
                                        const lineBreak = index === teacherEntries.length - 1 ? "" : "<br>";
                                        statsText += `• ${name}：${count} 门${lineBreak}`;
                                    });
                                }
                                appendWidgetMessage(statsText, 'bot');
                            }
                        } else {
                            // 其他回复
                            const reply = response.answer || "请输入'统计'查看课程信息";
                            appendWidgetMessage(reply, 'bot');
                        }
                    },
                    error: function(xhr, status, error) {
                        console.error("客服请求失败:", error);
                        appendWidgetMessage('抱歉，客服暂时无法连接，请稍后再试。', 'bot');
                    }
                });
            }

            function appendWidgetMessage(text, sender) {
                const chatMessages = document.getElementById('chatMessages');
                const messageDiv = document.createElement('div');
                messageDiv.className = `message ${sender}-message`;
                messageDiv.innerHTML = text;
                chatMessages.appendChild(messageDiv);
                chatMessages.scrollTop = chatMessages.scrollHeight;
            }

            function handleWidgetKeyPress(event) {
                if (event.key === 'Enter') {
                    sendWidgetMessage();
                }
            }

            // 初始化时确保聊天内容可见
            document.addEventListener('DOMContentLoaded', function() {
                document.getElementById('chatContent').style.display = 'block';
            });
        </script>

        <style>
            .chat-widget {
                position: fixed;
                bottom: 20px;
                right: 20px;
                width: 350px;
                background: rgba(43, 15, 65, 0.95);
                border-radius: 15px;
                box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
                border: 1px solid rgba(189, 136, 255, 0.3);
                backdrop-filter: blur(10px);
                z-index: 1000;
                overflow: hidden;
            }

            .chat-header {
                background: linear-gradient(135deg, var(--accent-violet) 0%, var(--accent-indigo) 100%);
                padding: 15px;
                cursor: pointer;
                display: flex;
                justify-content: space-between;
                align-items: center;
                font-weight: 500;
                color: white;
            }

            .chat-content {
                padding: 0;
            }

            .chat-messages {
                height: 250px;
                overflow-y: auto;
                padding: 15px;
                background: rgba(30, 10, 45, 0.7);
            }

            .chat-input {
                display: flex;
                gap: 10px;
                padding: 15px;
                border-top: 1px solid rgba(189, 136, 255, 0.2);
                background: rgba(43, 15, 65, 0.8);
            }

            .chat-input input {
                flex: 1;
                padding: 10px;
                border: none;
                border-radius: 8px;
                background: rgba(30, 10, 45, 0.7);
                color: var(--text-light);
                border: 1px solid rgba(189, 136, 255, 0.2);
            }

            .chat-input input:focus {
                outline: none;
                border-color: var(--accent-purple);
                box-shadow: 0 0 10px rgba(189, 136, 255, 0.4);
            }

            .chat-input button {
                padding: 10px 15px;
                background: linear-gradient(135deg, var(--accent-violet) 0%, var(--accent-indigo) 100%);
                color: white;
                border: none;
                border-radius: 8px;
                cursor: pointer;
                font-weight: 500;
                transition: all 0.3s ease;
            }

            .chat-input button:hover {
                transform: translateY(-2px);
                box-shadow: 0 5px 15px rgba(123, 44, 191, 0.6);
            }

            .message {
                padding: 10px 15px;
                margin-bottom: 10px;
                border-radius: 12px;
                max-width: 85%;
                word-wrap: break-word;
                font-size: 0.9rem;
                line-height: 1.4;
            }

            .user-message {
                background: linear-gradient(135deg, var(--accent-violet) 0%, var(--accent-indigo) 100%);
                margin-left: auto;
                text-align: right;
                color: white;
            }

            .bot-message {
                background: rgba(30, 10, 45, 0.8);
                margin-right: auto;
                border: 1px solid rgba(189, 136, 255, 0.2);
                color: var(--text-light);
            }
        </style>


<script src="/js/jquery.min.js"></script>
<script>
    $(function (){
        showAllCourse();
    })

    // 显示所有课程
    function showAllCourse(){
        $.ajax({
            url:"/minshuo/course/query",
            method:'get',
            dataType:'json',
            success:function (res){
                renderTable(res);
            }
        })
    }

    // 搜索课程
    function searchCourse(){
        var keyword = $("#searchKeyword").val();
        if(!keyword){
            showAllCourse();
            return;
        }
        $.ajax({
            url:"/minshuo/course/queryByCondition",
            method:'get',
            data: {keyword: keyword},
            dataType:'json',
            success:function (res){
                renderTable(res);
            }
        })
    }

    // 渲染表格 - 修改这个函数
    function renderTable(data){
        $("#tb").empty();
        var row =''
        $.each(data,function (i,v){
            row = row + "<tr>" +
                "<td><input type='checkbox' class='course-checkbox' data-id='" + v.id + "' style='display:none;'></td>" +
                "<td>" + v.id + "</td>" + // 显示数据库真实ID
                "<td>" + v.cname + "</td>" +
                "<td>" + v.mark + "</td>" +
                "<td>" + v.tea + "</td>" +
                "<td>" +
                "<button class='action-btn edit-btn' onclick='editCourse(" + v.id + ")'>编辑</button> " +
                "<button class='action-btn delete-btn' onclick='deleteCourse(" + v.id + ")'>删除</button>" +
                "</td></tr>"
        })
        $("#tb").append(row);

        // 根据批量模式状态决定是否显示复选框
        if(batchMode) {
            $(".course-checkbox").show();
            $("#selectAll").show();
        } else {
            $(".course-checkbox").hide();
            $("#selectAll").hide();
        }
    }

    // 删除课程
    function deleteCourse(id){
        if(confirm("确定要删除这门课程吗？")){
            $.ajax({
                url:"/minshuo/course/delete",
                method:'get',
                data: {id: id},
                success:function (res){
                    if(res > 0){
                        alert("删除成功");
                        showAllCourse();
                    }else{
                        alert("删除失败");
                    }
                }
            })
        }
    }

    // 编辑课程
    function editCourse(id){
        $.ajax({
            url:"/minshuo/course/getById",
            method:'get',
            data: {id: id},
            dataType:'json',
            success:function (res){
                $("#courseId").val(res.id);
                $("#cname").val(res.cname);
                $("#mark").val(res.mark);
                $("#tea").val(res.tea);
                $("#formTitle").text("编辑课程");
            }
        })
    }

    // 保存课程（添加或更新）
    function saveCourse(){
        var id = $("#courseId").val();
        var cname = $("#cname").val();
        var mark = $("#mark").val();
        var tea = $("#tea").val();

        if(!cname || !mark || !tea){
            alert("请填写完整信息");
            return;
        }

        var url = id ? "/minshuo/course/update" : "/minshuo/course/add";
        var data = {
            cname: cname,
            mark: mark,
            tea: tea
        };

        if(id){
            data.id = id;
        }

        $.ajax({
            url: url,
            method: 'post',
            data: data,
            success: function(res){
                if(res > 0){
                    alert("保存成功");
                    cancelEdit();
                    showAllCourse();
                }else{
                    alert("保存失败");
                }
            }
        })
    }

    // 取消编辑
    function cancelEdit(){
        $("#courseId").val("");
        $("#cname").val("");
        $("#mark").val("");
        $("#tea").val("");
        $("#formTitle").text("添加课程");
    }

    let batchMode = false;

    // 查看统计
    function showStatistics() {
        $.ajax({
            url: "/minshuo/course/statistics",
            method: 'get',
            dataType: 'json',
            success: function(res) {
                let stats = "课程统计信息:\n\n";
                stats += "总课程数: " + res.totalCourses + "\n\n";

                stats += "教师课程数:\n";
                for (let [teacher, count] of Object.entries(res.coursesByTeacher)) {
                    stats += teacher + ": " + count + "门\n";
                }

                alert(stats);
            }
        });
    }

    // 导出数据
    function exportCourses() {
        window.location.href = "/minshuo/course/export";
    }

    // 切换批量操作模式
    function toggleBatchMode() {
        batchMode = !batchMode;
        if (batchMode) {
            // 显示复选框
            $(".course-checkbox").show();
            $("#batchActions").show();
            $("#selectAll").show();
        } else {
            // 隐藏复选框
            $(".course-checkbox").hide();
            $("#batchActions").hide();
            $(".course-checkbox").prop('checked', false);
            $("#selectAll").prop('checked', false);
            $("#selectAll").hide();
        }
    }

    // 全选/取消全选
    function toggleSelectAll() {
        const isChecked = $("#selectAll").prop('checked');
        $(".course-checkbox").prop('checked', isChecked);
    }

    // 批量删除
    function batchDelete() {
        const selectedIds = [];
        $(".course-checkbox:checked").each(function() {
            selectedIds.push($(this).data('id'));
        });

        if (selectedIds.length === 0) {
            alert("请选择要删除的课程");
            return;
        }

        if (confirm("确定要删除选中的 " + selectedIds.length + " 门课程吗？")) {
            $.ajax({
                url: "/minshuo/course/batchDelete",
                method: 'post',
                traditional: true,
                data: {ids: selectedIds},
                success: function(res) {
                    if (res > 0) {
                        alert("成功删除 " + res + " 门课程");
                        showAllCourse();
                        toggleBatchMode(); // 退出批量模式
                    } else {
                        alert("删除失败");
                    }
                }
            });
        }
    }

    // 智能客服功能
    function toggleChatWidget() {
        const content = document.getElementById('chatContent');
        const icon = document.getElementById('chatToggleIcon');
        if (content.style.display === 'none') {
            content.style.display = 'block';
            icon.textContent = '▲';
        } else {
            content.style.display = 'none';
            icon.textContent = '▼';
        }
    }

    function sendWidgetMessage() {
        const messageInput = document.getElementById('widgetMessageInput');
        const message = messageInput.value.trim();

        if (!message) return;

        // 显示用户消息
        appendWidgetMessage(message, 'user');
        messageInput.value = '';

        // 发送请求到客服后端
        $.ajax({
            url: '/support/message',
            method: 'GET',
            data: { message: message },
            dataType: 'json',
            success: function(response) {
                console.log("客服返回:", response);

                if (response.action && response.action.type === "statistics") {
                    // 处理统计信息
                    const statsData = response.action.data;
                    if (statsData) {
                        const totalCourses = statsData.totalCourses || 0;
                        const coursesByTeacher = statsData.coursesByTeacher || {};

                        let statsText = "📊 课程统计信息<br><br>";
                        statsText += `<strong>总课程数：${totalCourses} 门</strong><br><br>`;
                        statsText += "<strong>各教师课程数量：</strong><br>";

                        const teacherEntries = Object.entries(coursesByTeacher);
                        if (teacherEntries.length === 0) {
                            statsText += "• 暂无教师数据";
                        } else {
                            teacherEntries.forEach(([name, count], index) => {
                                statsText += `• ${name}：${count} 门<br>`;
                            });
                        }
                        appendWidgetMessage(statsText, 'bot');
                    }
                } else {
                    // 其他回复
                    const reply = response.answer || "请输入'统计'查看课程信息";
                    appendWidgetMessage(reply, 'bot');
                }
            },
            error: function(xhr, status, error) {
                console.error("客服请求失败:", error);
                appendWidgetMessage('❌ 抱歉，客服暂时无法连接，请稍后再试。', 'bot');
            }
        });
    }

    function appendWidgetMessage(text, sender) {
        const chatMessages = document.getElementById('chatMessages');
        const messageDiv = document.createElement('div');
        messageDiv.className = `message ${sender}-message`;
        messageDiv.innerHTML = text;
        chatMessages.appendChild(messageDiv);
        chatMessages.scrollTop = chatMessages.scrollHeight;
    }

    function handleWidgetKeyPress(event) {
        if (event.key === 'Enter') {
            sendWidgetMessage();
        }
    }

    // 初始化
    document.addEventListener('DOMContentLoaded', function() {
        // 确保聊天内容默认显示
        const chatContent = document.getElementById('chatContent');
        if (chatContent) {
            chatContent.style.display = 'block';
        }
    });
</script>
</body>
</html>

